//
//  UIButton+BtnPadding.m
//  btn
//
//  Created by Xianjinkd on 2017/7/19.
//  Copyright © 2017年 Xianjinkd. All rights reserved.
//

#import "UIButton+BtnPadding.h"

@implementation UIButton (BtnPadding)
-(void)setImageDirection:(ImageDirection)direction withSpan:(float)span {
    UIImage *image = self.imageView.image;
    if(image){
        float imageWidth = image.size.width;
        float imageHeight = image.size.height;
        float titleWidth = self.titleLabel.bounds.size.width;
        float titleHeight = self.titleLabel.bounds.size.height;
        if(imageWidth == 0 || imageHeight == 0 || titleWidth == 0 || titleHeight == 0){
            return;
        }
        
        self.titleEdgeInsets = UIEdgeInsetsZero;
        self.imageEdgeInsets = UIEdgeInsetsZero;
        
        switch (direction) {
            case TOP:
                [self setImageEdgeInsets:UIEdgeInsetsMake(0.0, 0.0,titleHeight + (span/2),-titleWidth)];
                [self setTitleEdgeInsets:UIEdgeInsetsMake( imageHeight + (span/2),-imageWidth, 0.0,0.0)];
                break;
            case LEFT:
                [self setImageEdgeInsets:UIEdgeInsetsMake(0.0,0.0,0.0,span/2)];
                [self setTitleEdgeInsets:UIEdgeInsetsMake(0.0,span/2,0.0,0.0)];
                break;
            case RIGHT:
                [self setImageEdgeInsets:UIEdgeInsetsMake(0.0,0.0,0.0,-titleWidth*2 - (span/2))];
                [self setTitleEdgeInsets:UIEdgeInsetsMake(0.0,-imageWidth*2-(span/2),0.0,0.0)];
                break;
            case BOTTOM:
                [self setImageEdgeInsets:UIEdgeInsetsMake(titleHeight + (span/2),0.0,0.0,-titleWidth)];
                [self setTitleEdgeInsets:UIEdgeInsetsMake(0.0,-imageWidth,imageHeight + (span/2),0.0)];
                break;
            case CENTER:
                [self setImageEdgeInsets:UIEdgeInsetsMake(0.0,0.0,0.0,-titleWidth)];
                [self setTitleEdgeInsets:UIEdgeInsetsMake(0.0,-imageWidth,0.0,0.0)];
                break;
            default:
                break;
        }
    }
}

- (void)viewCorner {
//    UIView设置部分圆角（左上，右上，左下，右下）
    
//    CGRect rect = view.bounds;
//    CGSize radio = CGSizeMake(30, 30);//圆角尺寸
//    UIRectCorner corner = UIRectCornerTopLeft|UIRectCornerTopRight;//这是圆角位置
//    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corner cornerRadii:radio];
//    CAShapeLayer *masklayer = [[CAShapeLayer alloc]init];//创建shapelayer
//    masklayer.frame = view.bounds;
//    masklayer.path = path.CGPath;//设置路径
//    view.layer.mask = masklayer;
}

@end
